//
//  订单提交Ctr-cells.swift
//  Aiwei
//
//  Created by zhu xietong on 2017/11/8.
//  Copyright © 2017年 zhu xietong. All rights reserved.
//

import Foundation
import Eelay
import soapp



public enum IntegralMode {
    case none
    case full(Int,open:Bool)
    case integral(Int,open:Bool)
    
    
    var count:Int{
        get{
            switch self {
            case .full(let n, open: _),.integral(let n, open: _):
                return n
            default:
                return 0
            }
        }
    }
    
    public var description:NSAttributedString{
        get{
            switch self {
            case .none:
                return "不支持积分兑换".ui.font12.color(color: ._weak).astring
            case .integral:
                return "开启".ui.color(color: ___shop.tint).font12.astring + " \(count)积分".ui.bfont14.color(color: ___shop.tint).astring + " + 现金兑换购买".ui.color(color: ___shop.tint).font12.astring
            case .full:
                return "使用".ui.color(color: ___shop.tint).font12.astring + " \(count)积分".ui.bfont14.color(color: ___shop.tint).astring + "全款抵扣".ui.color(color: ___shop.tint).font12.astring
            }
        }
    }
    
    public func priceDiscription(obj:NSMutableDictionary) ->NSAttributedString{
        switch self {
        case .none:
            return "不支持积分兑换".ui.font12.color(color: ._weak).astring
        case .integral:
            return "开启".ui.color(color: ___shop.tint).font12.astring + " \(count)积分".ui.bfont14.color(color: ___shop.tint).astring + " + 现金兑换购买".ui.color(color: ___shop.tint).font12.astring
        case .full:
            return "使用".ui.color(color: ___shop.tint).font12.astring + " \(count)积分".ui.bfont14.color(color: ___shop.tint).astring + "全款抵扣".ui.color(color: ___shop.tint).font12.astring
        }
        
    }
    
    public func price(obj:NSMutableDictionary) ->NSAttributedString{
        switch self {
        case .none:
            return "不支持积分兑换".ui.font12.color(color: ._weak).astring
        case .integral:
            return "开启".ui.color(color: ___shop.tint).font12.astring + " \(count)积分".ui.bfont14.color(color: ___shop.tint).astring + " + 现金兑换购买".ui.color(color: ___shop.tint).font12.astring
        case .full:
            return "使用".ui.color(color: ___shop.tint).font12.astring + " \(count)积分".ui.bfont14.color(color: ___shop.tint).astring + "全款抵扣".ui.color(color: ___shop.tint).font12.astring
        }
        
    }
    
    
    
}

extension SCOrderSubmitCtr{
    
    
    
    class SubmitBar:EeBottomBar{
        let priceL = UILabel()
        let confirmBt = UIButton()
        var intergalMode = IntegralMode.none{
            didSet{
                mo_model_reload()
            }
        }
    
        
        
        override var contentHeight: CGFloat{
            get{
                return 45
            }
        }
        
        override func addLayoutRules() {
            let line = UIView()
            contentView.eelay = [
                [confirmBt,[ee.T.B.R],122],
                [priceL,[confirmBt,ee.L,ee.R,-30],[ee.Y]],
                [line,[ee.T.L.R],"\(TP.separatorHeight)"],
            ]
            line.backgroundColor = UIColor._separator
            _ = confirmBt.ui.ncolor(hex: "#fff").font(size: 14).ntitle(title: "\("提交".loc)\(OrderScheme.default.name)")
            confirmBt.setBackgroundImage(UIImage(named: "shop_buy"), for: .normal)
            confirmBt.contentEdgeInsets = [8,16]
            priceL.attributedText = "\("合计".loc)：".ui.font13.color(hex: "#111").astring + "¥\(129)".ui.color(color: ___shop.tint).font15.astring
        }
        
        override func mo_model_reload() {
            
            switch intergalMode {
            case .full(let n, open: let op):
                if(op){
                let priceStr = mo.model[float:"realprice",0].price_display
                priceL.attributedText = "消耗 \(n) 积分".ui.bfont13.color(color: ___shop.tint).astring
                    return
                }
                
            case .integral(let n, open: let op):
                if(op){
                    let priceStr = mo.model[float:"realprice",0].price_display
                    priceL.attributedText = "\("合计".loc)：".ui.font13.color(hex: "#111").astring + "\("¥".loc) \(priceStr)".ui.color(color: ___shop.tint).bfont14.astring + " + ".ui.font12.color(color: ___shop.tint).astring + "\(n) 积分".ui.bfont14.color(color: ___shop.tint).astring
                    return
                }
                
               
            default:
                break
            }
            let priceStr = mo.model[float:"realprice",0].price_display
            priceL.attributedText = "\("合计".loc)：".ui.font13.color(hex: "#111").astring + "\("¥".loc) \(priceStr)".ui.color(color: ___shop.tint).font16.astring
            
//            let priceStr = mo.model[float:"realprice",0].price_display
//            priceL.attributedText = "\("合计".loc)：".ui.font13.color(hex: "#111").astring + "\("¥".loc) \(priceStr)".ui.color(color: ___shop.tint).font16.astring
            
        }
    }
    
    
    //    class NoneAdressCell: JoTableCell {
    //        let iconV = UIImageView()
    //        let nameL = UILabel()
    //        let addressL = UILabel()
    //        let fixL = UILabel()
    //
    //        override func addLayoutRules() {
    //            let r = AppTheme.edge + 10
    //
    //            let indicatorV = UIImageView()
    //            let bt = UIButton()
    //            contentView.eelay = [
    //                [__line,[ee.T.L.B],"94"],
    //                [iconV,[ee.Y.L,[0,AppTheme.edge]],"21",17],
    //                [addressL,[ee.Y],[iconV,ee.R,ee.L,10],[ee.R,-r]],
    //                [nameL,[addressL,ee.T,ee.B,-6],[iconV,ee.R,ee.L,10],[ee.R,-r]],
    //                [fixL,[addressL,ee.B,ee.T,6],[iconV,ee.R,ee.L,10],[ee.R,-r]],
    //                [indicatorV,[ee.R.Y,[-(AppTheme.edge-10),0]],"20",20],
    //                [bt,[ee.T.L.B.R]],
    //            ]
    //
    //
    //            indicatorV.image = #imageLiteral(resourceName: "f_more_indicator")
    //            iconV.image = UIImage(named:"zhifu_dizhi")
    //            addressL.ui.font14.line(2).cl_888.text(__titles.creat_info(10))
    //            nameL.ui.font16.cl_000.text("\(__names.one_str) 17888383839")
    //            fixL.ui.color(hex: UIColor.main_hex).font14.text("点击修改默认地址")
    //            //            self.accessoryType = .disclosureIndicator
    //
    //        }
    //        @objc func selectAdress()
    //        {
    //            let ctr = AddressListCtr()
    //            self.content_controller?.navigationController?.pushViewController(ctr, animated: true)
    //        }
    //    }
    
    public class AdressCell: JoTableCell {
        public let iconV = UIImageView()
        public let nameL = UILabel()
        public let addressL = UILabel()
        public let fixL = UILabel()
        
        public let addressV = UIView()
        public let emptyV = UIView()
        
        public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
            iconV.image = UIImage(named:"dizhi")?.dynamicImage(color: ._foregroundWeak)
        }
        
        public override func addLayoutRules() {
            let r = AppTheme.edge + 10
            let indicatorV = UIImageView()
            let bt = UIButton()
            
            addressV.eelay = [
                [__line,[ee.T.L.B],"94"],
                [iconV,[ee.Y.L,[0,AppTheme.edge]],"23",17],
                [addressL,[ee.Y],[iconV,ee.R,ee.L,10],[ee.R,-r]],
                [nameL,[addressL,ee.T,ee.B,-6],[iconV,ee.R,ee.L,10],[ee.R,-r]],
                [fixL,[addressL,ee.B,ee.T,6],[iconV,ee.R,ee.L,10],[ee.R,-r]],
                [indicatorV,[ee.R.Y,[-(AppTheme.edge-10),0]],"20",20],
            ]
            let emptyL = UILabel()
            emptyV.eelay = [
                [emptyL,[ee.X.Y]],
            ]
            emptyL.ui.font16.center.text("编辑收货信息".loc)
            emptyV.backgroundColor = ___shop.tint
            emptyV.alpha = 0.25
            
            
            contentView.eelay = [
                [addressV,[ee.T.L.B.R]],
                [emptyV,[ee.T.L.B.R]],
                [bt,[ee.T.L.B.R]],
            ]
            
            
            indicatorV.image = UIImage(named: "f_more_indicator")
            iconV.image = UIImage(named:"dizhi")?.dynamicImage(color: ._foregroundWeak)
            addressL.ui.font12.line(2).text(color: ._foregroundWeak).text("")
            nameL.ui.font15.text(color: ._foreground).text("")
            fixL.ui.text(color: ___shop.tint).bfont11.text("点击修改地址".loc)
            //            self.accessoryType = .disclosureIndicator
            
            bt.addTarget(self, action: #selector(selectAdress), for: .touchUpInside)
        }
        
        var haveValidAdress:Bool{
            get{
                if mo.model["address.mobile",""].len > 0{
                    return true
                }
                return false
            }
        }
        
        public override func mo_model_reload() {
            if haveValidAdress{
                let address = DataNode.path(node: "address", mo.model, value: NSMutableDictionary())
                
                let mobile = address["mobile",""].replacingOccurrences(of: "|", with: " ").replacingOccurrences(of: "+", with: " ")
                addressL.text = GlobalPlaceField.displayStr(address: address)
                nameL.text = address["realname",""] + " "  + mobile
                
                
                addressV.alpha = 1
                emptyV.alpha = 0
            }
            else{
                addressV.alpha = 0
                emptyV.alpha = 1
            }
            
        }
        
        @objc public func selectAdress()
        {
            weak var wself = self
            if haveValidAdress
            {
                let ctr = AddressListCtr()
                let currentAddress = DataNode.path(node: "address", mo.model, value: NSMutableDictionary())
                ctr.observerListChange = {
                    change in
                    if let ws = wself{
                        switch change{
                        case .delete(let item):
                            
                            if currentAddress["id","XXX"] == item["id","***"]
                            {
                                ws.mo.model["address"] = NSMutableDictionary()
                                ws.mo_model_reload()
                            }
                        case .update(let item):
                            if currentAddress["id","XXX"] == item["id","***"]
                            {
                                ws.mo.model["address"] = item
                                ws.mo_model_reload()
                            }
                        default:
                            break
                        }
                    }
                }
                ctr.selectAddress = { address in
                    wself?.mo.model["address"] = address
                    wself?.mo_model_reload()
                }
                self.content_controller?.navigationController?.pushViewController(ctr, animated: true)
            }
            else{
                let ctr = AddressEditCtr()
                ctr.editType = .add(nil)
                ctr.successAction = {
                    type,address in
                    wself?.mo.model["address"] = address
                    wself?.mo_model_reload()
                }
                self.content_controller?.navigationController?.pushViewController(ctr, animated: true)
            }
        }
    }
    
    class WuliuCell:JoTableCell{
        let nameL = UILabel()
        let priceL = UILabel()
        override func addLayoutRules() {
            contentView.eelay = [
                [nameL,[ee.T.L.B,[14,AppTheme.edge,-14]]],
                [priceL,[nameL,ee.Y],[ee.R,-AppTheme.edge]],
                [__line,[ee.L.B.R],"\(TP.separatorHeight)"],
            ]
            __line.backgroundColor = UIColor._separator
        }
        override func mo_model_reload() {
            nameL.ui.font14.text(color: ._foreground).text("邮费".loc)
            
            let price = mo.model[float:Goods.key_wuliu_price,0]
            if price > 0
            {
                priceL.ui.font13.right.cl_888.text("¥ \(price.price_display)")
                
            }
            else{
                priceL.ui.font13.right.text(color: ._foregroundWeak).text("免邮费".loc)
            }
        }
    }
    
    
    class SellerCell: JoTableCell {
        let iconV = UIImageView()
        let nameL = UILabel()
        override func addLayoutRules() {
            let line = UIView()
            contentView.eelay = [
                [line,[ee.T.L.R],"6"],
                [iconV,[ee.T.L.B,[20,AppTheme.edge,-14]],"16",16],
                [nameL,[iconV,ee.R.Y,ee.L.Y,[10,0]],[ee.R,-AppTheme.edge]],
                [__line,[ee.L.B.R],"\(TP.separatorHeight)"]
            ]
            line.backgroundColor = UIColor._tableBackground
            __line.backgroundColor = UIColor._separator
            _ = nameL.ui.font12.text(color: ._foreground).left
        }
        
        override func mo_model_reload() {
            iconV.img_url = mo.model["logo",""]
            nameL.text = mo.model["merchname",""]
        }
    }
    
    class OneSellerPriceCell:JoTableCell{
        let priceL = UILabel()
        
        let nameL = UILabel()
        override func addLayoutRules() {
            contentView.eelay = [
                [priceL,[ee.T.R.B,[14,-AppTheme.edge,-14]]],
                [nameL,[priceL,ee.Y],[priceL,ee.L,ee.R,-8]],
            ]
        }
        
        override func mo_model_reload() {
            let text = "共计[XX] 件[YY]".replaceVars(values: mo.model["__model.goodsnum","1"],OrderScheme.default.goodsTag)
            nameL.ui.font12.text(color: ._foreground).text(text)
            let price = mo.model[float:"__model.totalprice",0]
            priceL.ui.font15.right.text(color: ___shop.second).text("\("¥".loc) \(price.price_display)")
        }
    }
    
    class GoodsCell: JoTableCell {
        
        let imgV = UIImageView()
        let titleL = Goods.Label()
        let tagsL = UILabel()
        let priceL = UILabel()
        let numL = UILabel()
        
        override func addLayoutRules() {
            let backV = UIView()
            backV.eelay = [
                [imgV,[ee.T.L.B,[0,0,0]],"74",74],
                [priceL,[ee.R],[imgV,ee.T,0]],
                [titleL,[imgV,ee.R,ee.L,8.+1000],[imgV,ee.T,0],[priceL,ee.L,ee.R,-8]],
                [numL,[ee.R],[imgV,ee.B]],
                [tagsL,[imgV,ee.R,ee.L,8],[imgV,ee.B,0],[ee.R,-70]],
                [numL,[ee.R],[tagsL,ee.Y]],
                //            [__line,[ee.L.B.R,[0,0.0]],"0.5"]
            ]
            
            contentView.eelay = [
                [backV,[ee.T.L.B.R,[10,AppTheme.edge,-10,-AppTheme.edge]]],
                [__line,[ee.L.B.R],"\(TP.separatorHeight)"]
            ]
            __line.backgroundColor = UIColor._separator
            //        __line.backgroundColor = FieldCell.separator_color
            
            imgV.img_url = __goods.one_str
            //            titleL.ui.font13.cl_222.text(__goodsNames.one_str)
            tagsL.ui.font12.text("颜色：黑")
            priceL.ui.font12.text(color: ._foreground).right.text("¥ 120")
            numL.ui.font12.text(color: ._foregroundWeak).right.text("X1")
            priceL.setContentConstrainToHighLever()
        }
        override func mo_model_reload() {
            imgV.img_url = mo.model["thumb",""]
            
            titleL.tagStr = ""
            titleL.text = mo.model["title",""]
            tagsL.text = mo.model["optiontitle",""]
            priceL.attributedText = "¥ ".ui.font11.color(color: ___shop.second).astring +  mo.model[float:"marketprice",0].price_display.ui.font13.color(color: ___shop.second).astring
            numL.attributedText = "X ".ui.font10.cl_999.astring + mo.model["total","1"].ui.font13.cl_666.astring
        }
    }
    
    class DiscountInfoCell:JoTableCell{
        let vipNameL = UILabel()
        let reducePriceL = UILabel()
        override func addLayoutRules() {
            
            contentView.eelay = [
                [vipNameL,[ee.T.L.B,[12,AppTheme.edge,-12]]],
                [reducePriceL,[ee.R,-AppTheme.edge],[ee.Y]],
                [__line,[ee.L.B.R],"\(TP.separatorHeight)"]
            ]
            __line.backgroundColor = UIColor.separator_color
            vipNameL.ui.font14.cl_444.text("当前会员等级".loc)
            _ = reducePriceL.ui.font14.cl_222
            contentView.backgroundColor = UIColor(shex:"\(UIColor.main_hex)22")
        }
        
        override func mo_model_reload() {
            vipNameL.text = "会员享受折扣优惠".loc
            var price = mo.model[float:"__model.price_info.discount_price",0]
            if price == 0
            {
                price = mo.model[float:"__model.discount_price",0]
            }
            reducePriceL.attributedText = "减免".loc.ui.font13.cl_888.astring + "   ¥ \(price.price_display)".ui.font13.color(hex: UIColor.main_hex).astring
        }
    }
    
    class OfflineNoteCell: JoTableCell {
        let titleL = UILabel()
        override func addLayoutRules() {
            contentView.eelay = [
                [titleL,[ee.T.L.B.R,[20,AppTheme.edge,-10,-AppTheme.edge]]],
            ]
            contentView.backgroundColor = UIColor._tableBackground
            titleL.ui.font12.cl_888.text("该订单可到线下消费，请填写消费人姓名、联系电话".loc)
        }
    }
    
    //    class GoodsItemCell: JoTableCell {
    //        let imgV = UIImageView()
    //        let titleL = UILabel()
    //        let tagsL = UILabel()
    //        let priceL = UILabel()
    //        let numL = UILabel()
    //        override func addLayoutRules() {
    //            contentView.eelay = [
    //                [imgV,[ee.T.L.B,[12,AppTheme.edge,-12]],"50",50],
    //                [titleL,[imgV,ee.R,ee.L,8],[imgV,ee.T,4],[ee.R,-100]],
    //                [tagsL,[imgV,ee.R,ee.L,8],[imgV,ee.B,-4],[ee.R,-100]],
    //                [priceL,[ee.R,-AppTheme.edge],[titleL,ee.Y]],
    //                [numL,[ee.R,-AppTheme.edge],[tagsL,ee.Y]],
    //                [__line,[ee.L.B.R,[0,0.0]],"0.5"]
    //            ]
    //            __line.backgroundColor = FieldCell.separator_color
    //            imgV.img_url = __goods.one_str
    //            titleL.ui.font14.cl_222.text(__goodsNames.one_str)
    //            tagsL.ui.font13.cl_999.text("颜色：黑")
    //            priceL.ui.font13.cl_222.right.text("¥ 120")
    //            numL.ui.font13.cl_999.right.text("X1")
    //        }
    //    }
    //
    
}

